<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <!-- 放置一个el-tree组件 -->
      <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
        <template #default="{ data }">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            style="height: 40px; width: 100%;"
          >
            <el-col :span="20">
              <span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <el-col>
                  <!-- 下拉菜单 element -->
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <!-- 下拉菜单 -->
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDepartment(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="editDepart(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item @click.native="del(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>

    </div>
    <addDapart
      ref="addDepart"
      :cur-id="curId"
      :show-dialog="showDialog"
      :all-list="allList"
      @close-dialog="handleClose"
      @upDate="hGetDepartments"
    />
  </div>
</template>

<script>
// 引入接口
import { getDepartments, delDepartments } from '@/api/departments'
// import addDapart from './components/add-department.vue'

import { transTree } from '@/utils/transTreeData'

export default {
  components: {
    // addDapart
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [{
        name: '财务部',
        children: [
          {
            name: '财务核算部'
          },
          {
            name: '税务核算部'
          }
        ]
      }],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      curId: '',
      allList: []
    }
  },
  // 调用method函数
  mounted() {
    this.hGetDepartments()
  },
  // 封装专门的method函数
  methods: {
    async hGetDepartments() {
      const res = await getDepartments()
      // 把数组中code字段单独拿出来作为一个新数组
      this.allList = res.depts.map(item => item.code)

      this.list = transTree(res.depts)
    },
    del(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async() => {
        // 删除操作成功
        await delDepartments(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.hGetDepartments()
      }).catch(error => {
        console.log(error)
      })
    },
    // 新增
    addDepartment(id) {
      this.curId = id

      this.showDialog = true
    },
    // 编辑
    editDepart(id) {
      // 1.打开弹框
      this.showDialog = true
      this.curId = id
      // 3.通知子组件进行数据获取回显
      this.$refs.addDepart.getDetail(id)
    },
    // 子传父关闭弹框
    handleClose(flag) {
      this.showDialog = flag
    }
  }
}
</script>

<style lang="less" scoped>
</style>
